package com.wms.task;

import com.wms.entity.WmsGoods;
import com.wms.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
@Slf4j
public class WebSocketTask {
    @Autowired
    private GoodsService goodsService;

    /**
     * 通过Spring Task每隔1分钟检查库存数量
     */
    @Scheduled(cron = "0 * * * * ?") //每分钟触发一次
    public void processStatusGoods() {
        log.info("定时检查库存状态,{}", LocalDateTime.now());
        List<WmsGoods> goods = goodsService.getAll();
        for (WmsGoods good : goods) {
            if (good.getCount() < 100){
                good.setStatus(1);
                goodsService.update(good);
            }else{
                good.setStatus(0);
                goodsService.update(good);
            }
        }
    }
}
